package controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import model.Kunde;
import model.Zimmer;
import view.CustomersListView;
import view.RoomsListView;

public class GuiCustomersListController extends GUIAbstractController implements ActionListener
{
	private CustomersListView customersView;
	private CustomersTableModel customersModel;
	private Container container;
	
	public GuiCustomersListController(Container container){
		
		this.container = container;
		
		customersView = new CustomersListView();
		customersView.addActionListener(this);
		
		reloadRoomsTable();
	}

	private void reloadRoomsTable()
	{
		List<Kunde> customers = super.dbController.loadAllEntities(new Kunde());
		JTable table = customersView.getTableCustomer();
		customersModel = new CustomersTableModel(customers);
		
		table.setModel(customersModel);
		table.repaint();
	}

	public CustomersListView  getCustomersView()
	{
		return customersView;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		int index = customersView.getTableCustomer().getSelectedRow();
		Kunde selectedCustomer = customersModel.getCustomers().get(index);
		
		if(e.getSource() == customersView.getBtnEdit()){
			
			GUICreateCustomerViewController customerEditViewController = new GUICreateCustomerViewController(selectedCustomer);
			container.removeAll();
			container.add(customerEditViewController.getCustomerView());
			container.setSize(customerEditViewController.getCustomerView().getSize());
			container.validate();
			container.repaint();
		}else if(e.getSource() == customersView.getBtnDelete()){
			super.dbController.deleteEntity(selectedCustomer);
			reloadRoomsTable();
		}
		
	}
	
	
}